package com.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.dao.BrandDao;
import com.changgou.goods.dao.CategoryDao;
import com.changgou.goods.dao.SkuDao;
import com.changgou.goods.dao.SpuDao;
import com.changgou.goods.service.SpuService;
import com.haha.goods.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Yang
 * @date 2020/11/7
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuDao, Spu> implements SpuService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private SkuDao skuDao;
    @Override
    @Transactional
    public void saveGoods(Goods goods) {
        //增加spu
        Spu spu = goods.getSpu();
        if (spu.getId()!=null){
            baseMapper.updateById(spu);
            //删除改spu下的所有sku，然后重新添加
            QueryWrapper<Sku> wrapper = new QueryWrapper<>();
            wrapper.eq("spu_id",spu.getId());
            skuDao.delete(wrapper);
        }else {
            //新增spu
            baseMapper.insert(spu);
        }

        //处理sku
        List<Sku> skuList = goods.getSkuList();
        Category category = categoryDao.selectById(spu.getCategory3Id());
        Brand brand = brandDao.selectById(spu.getBrandId());
        for (Sku sku : skuList) {
            //构建SKU名称，采用SPU+规格值组装
            if(StringUtils.isEmpty(sku.getSpec())){
                sku.setSpec("{}");
            }
            String name = spu.getName();
            Map<String,String> map = JSON.parseObject(sku.getSpec(), Map.class);
            //循环组装Sku的名字
            for (Map.Entry<String, String> entry : map.entrySet()) {
                name = name+" "+entry.getValue();
            }
            sku.setName(name);
            Date date = new Date();
            //SpuId
            sku.setSpuId(spu.getId());
            //创建日期
            sku.setCreateTime(date);
            //修改日期
            sku.setUpdateTime(date);
            //商品分类ID
            sku.setCategoryId(spu.getCategory3Id());
            //分类名字
            sku.setCategoryName(category.getName());
            //品牌名字
            sku.setBrandName(brand.getName());
            skuDao.insert(sku);
        }


    }

    @Override
    public Goods findGoodsById(Long id) {
        Goods goods = new Goods();
        Spu spu = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(spu)){
            return null;
        }
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spu.getId());
        List<Sku> skuList = skuDao.selectList(queryWrapper);
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }

    @Override
    public void audit(Long id) {
        //查询商品
        Spu spu = baseMapper.selectById(id);
        //判断商品是否已经删除
        if(spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("该商品已经删除！");
        }
        //实现上架和审核
        spu.setStatus("1"); //审核通过
        spu.setIsMarketable("1"); //上架
       baseMapper.updateById(spu);
    }

    @Override
    public void pull(Long id) {
        Spu spu = baseMapper.selectById(id);
        //判断商品是否已经删除
        if(spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("该商品已经删除！");
        }
        //实现上架和审核
        spu.setIsMarketable("0"); //下架
        baseMapper.updateById(spu);
    }

    @Override
    public void put(Long id) {
        Spu spu = baseMapper.selectById(id);
        //判断商品是否已经删除
        if(spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("该商品已经删除！");
        }
        if (!"1".equals(spu.getStatus())){
            throw new RuntimeException("审核没通过或者待审核");
        }
        //实现上架和审核
        spu.setIsMarketable("1"); //上架
        baseMapper.updateById(spu);
    }

    @Override
    @Transactional
    public int putMany(Long[] ids) {
        for (Long id : ids) {
            put(id);
        }
        return ids.length;
    }

    @Override
    public void logicDelete(Long id) {
        Spu spu = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(spu)){
            throw new RuntimeException("该商品的id不正确");
        }
        if (!"0".equals(spu.getIsMarketable())){
            throw new RuntimeException("必须先下架才能够删除");
        }
        spu.setIsDelete("1");
        baseMapper.updateById(spu);
    }

    @Override
    public void restore(Long id) {
        Spu spu = baseMapper.selectById(id);
        if (!"1".equals(spu.getIsDelete())){
            throw new RuntimeException("该商品没有删除");
        }
        spu.setIsDelete("0");
        baseMapper.updateById(spu);
    }
}
